package states
{
	import core.Assets;
	import core.Game;
	
	import interfaces.IState;
	
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	
	public class Menu extends Sprite implements IState
	{
		private var _game:Game;
		private var _logo:Image;
		private var _playButton:Button;
		
		public function Menu(game:Game)
		{
			trace("Menu initialized.");
			this._game = game;
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			super();
		}
		
		private function init(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			
			/**
			 * Display the logo.
			 * The logos pivot is centered so now positioning the logo will be based off its center and not the 
			 * default top-left corner.
			 * */
			_logo = new Image(Assets.atlasTexture.getTexture("spacer-title"));
			this.addChild(_logo);
			_logo.pivotX = _logo.texture.width * 0.5;
			_logo.pivotY = _logo.texture.height * 0.5;
			_logo.scaleX = _logo.scaleY = 2;
			_logo.x = stage.stageWidth * 0.5;
			_logo.y = 250;
			
			/**
			 * Display play button.
			 * Pivot at center.
			 * */
			_playButton = new Button(Assets.atlasTexture.getTexture("play-button"));
			_playButton.pivotX = _playButton.width * 0.5;
			_playButton.pivotY = _playButton.height * 0.5;
			_playButton.x = stage.stageWidth * 0.5;
			_playButton.y = 400;
			this.addChild(_playButton);
			_playButton.addEventListener(Event.TRIGGERED, onPlayButtonClicked);
		}
		
		public function update(deltaSmall:Number):void
		{
			
		}
		
		public function destroy():void
		{
			_game = null;
			
			removeChild(_logo);
			_logo.dispose();
			
			_playButton.removeFromParent(true);
			
			/**
			 * Have to make sure that the state class is removed and disposed.
			 * Since the state classes are added to the Game Class, .removeFromParent() works well.
			 * */
			//this.removeFromParent(true); // will be removed and disposed by the parent
		}
		
		private function onPlayButtonClicked(e:Event):void
		{
			this.removeEventListener(Event.TRIGGERED, onPlayButtonClicked);
			
			_game.switchState(Game.PLAY_STATE);
		}
	}
}



































